/**
*市电引入js
*/ 

var	mainssupplyDS;
var mainssupplyFormPanel;
function doMainssupplyDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JMainssupplyWeb.deleteMainsSupplyBO(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					mainssupplyDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//mainssupplyFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var mainssupplyType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'type', type: "string"},
							   {name: 'elecableTDLen', type: "string"},
							   {name: 'elecableTDM', type: "string"},
							   {name: 'elecableTDT',  type: 'string'},
							   {name: 'elecableTDP', type: "string"},
							   {name: 'elecableLDLen', type: "string"},
							   {name: 'elecableLDM', type: 'string'},
							   {name: 'elecableLDT', type: 'string'},
							   {name: 'elecableLDP',  type: 'string'},
							   {name: 'maintenance',  type: 'string'},
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'}
						 ]); 
	var mainssupplyCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "市电类型",  sortable: true, dataIndex: 'type',align:'left'},	 				
				{header: "删除", dataIndex: 'id',renderer: showDelete}					
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doMainssupplyDelete(\"" + id  +   "\")'>";  
	}
	
	
	
	mainssupplyDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JMainssupplyWeb.getMainsSupplyList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, mainssupplyType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var mainssupplyGrid = new Ext.grid.GridPanel({			 	
			ds: mainssupplyDS,
			cm: mainssupplyCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            mainssupplyFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: mainssupplyDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});
		
	mainssupplyDS.on('load', function() {
		mainssupplyGrid.getSelectionModel().selectFirstRow(); 
	});		

	var typeStore  = new Ext.data.Store({  
			proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
			reader:new Ext.data.DWRArrayReader(
					{},
					new Ext.data.Record.create([  
						{name: 'text', mapping:1},       
						{name: 'value',mapping:0}                   
					  ])
					)  
		});
	var typeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '市电类型',
		name: 'type',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'type',
		triggerAction: 'all', 
		editable:false, 
		store: typeStore
	});
	typeStore.on('beforeload', function() {
			  typeStore.baseParams = {
					DIRECTORY_TYPE:'14'   
			  };
		});	
	typeStore.load({params:{DIRECTORY_TYPE:'14'}});		

	// Setup the form panel
	mainssupplyFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',  
		height: 200,
		minSize: 200,
		maxSize: 250,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			{
				layout:'column', 
				items:[
					 {
						columnWidth:.33,
						layout: 'form', 
						labelAlign:'right',		
						labelWidth:70,	 
						items: [
							{
								xtype:'hidden',
								fieldLabel: '流水号',
								width:150,
								name: 'id'
							}, {
								xtype:'textfield', 
								fieldLabel: '基站编号', 
								width:150,
								name: 'btscode'
							}]
					 },
					 {
						columnWidth:.33,
						layout: 'form',  
						labelAlign:'right',	
						labelWidth:70,	 
						items: [
							{ 
								xtype:'textfield',
								fieldLabel: '基站名称',
								width:150,
								readOnly: true,
								name: 'btsname'
							}]
					 },
					 {
						columnWidth:.34,
						layout: 'form',  
						labelWidth:70,	
						labelAlign:'right',			
						items: typeCombo
					 }
				]
			},
			{
				xtype:'tabpanel',
				plain:true,
				activeTab: 0,
				height:120, 
				deferredRender: false,
				defaults:{bodyStyle:'padding:10px'},
				items:[
					{
						title:'变压器至机房配电箱市电线缆数据',
						frame:true,
						layout:'column', 
						items:[
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:100,	 
								items: [
									{
										xtype:'numberfield', 
										decimalPrecision:1,
										value:1.0,
										fieldLabel: '电缆长度（米）', 										
										width:100,
										name: 'elecableTDLen'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form', 
								labelAlign:'right',		
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield',
										fieldLabel: '电缆规格',
										width:100,
										name: 'elecableTDM'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield',
										fieldLabel: '电缆类型',
										width:100, 
										name: 'elecableTDT'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield',
										fieldLabel: '电缆产权',
										width:100,
										name: 'elecableTDP'
									}]
							 }
						]
					},{
						title:'低压搭接点至机房配电箱市电线缆数据',
						frame:true,
						layout:'column', 
						labelAlign :'center',
						items:[
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:100,	 
								items: [
									{
										xtype:'numberfield', 
										decimalPrecision:1,
										value:1.0,
										fieldLabel: '电缆长度（米）', 
										width:100,
										name: 'elecableLDLen'
									},{
										xtype:'textfield', 
										fieldLabel: '电力线缆整治及更换记录', 
										width:100,
										name: 'maintenance'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form', 
								labelAlign:'right',		
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield',
										fieldLabel: '电缆规格',
										width:100,
										name: 'elecableLDM'
									},{
										xtype:'textarea',
										fieldLabel: '备注',
										width:100,
										name: 'memo'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form', 
								labelAlign:'right',		
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield',
										fieldLabel: '电缆类型',
										width:100,
										name: 'elecableLDT'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield',
										fieldLabel: '电缆产权',
										width:100,
										name: 'elecableLDP'
									}]
							 }
						]
					}]
			}	 
			],
		buttons :[
			{
				text : ' 保存',
				handler : saveMainssupply
			},{
				text : ' 新建/取消',
				handler : function(){
					mainssupplyFormPanel.getForm().reset(); 
					mainssupplyFormPanel.getForm().loadRecord(recordForAll);	
				}
			}
			]	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'MainssupplyTab',
		items    : [
			mainssupplyGrid,
			mainssupplyFormPanel
		]
	});
	
	
	function saveMainssupply()
	{
		if (!mainssupplyFormPanel.getForm().isValid())
			return;	
			
		var form = mainssupplyFormPanel.getForm().getValues(false);	
		MainssupplyBO = {};
		MainssupplyBO.id = form.id;
		MainssupplyBO.btscode = form.btscode;
		MainssupplyBO.btsname= form.btsname;
		MainssupplyBO.type = form.type;
		MainssupplyBO.elecableTDLen = form.elecableTDLen;
		MainssupplyBO.elecableTDM = form.elecableTDM;
		MainssupplyBO.elecableTDT = form.elecableTDT;
		MainssupplyBO.elecableTDP = form.elecableTDP;
		MainssupplyBO.elecableLDLen = form.elecableLDLen;
		MainssupplyBO.elecableLDM = form.elecableLDM;
		MainssupplyBO.elecableLDT = form.elecableLDT;
		MainssupplyBO.elecableLDP = form.elecableLDP;
		MainssupplyBO.maintenance = form.maintenance;
		MainssupplyBO.memo = form.memo; 
			
		JMainssupplyWeb.saveMainsSupplyBO(MainssupplyBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(mainssupplyDS,mainssupplyFormPanel); 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
	
	loadDate(mainssupplyDS,mainssupplyFormPanel);
		
	
})